package com.cr.sync.service.impl;

import com.alibaba.fastjson2.JSON;
import com.cr.common.utils.StringUtils;
import com.cr.sync.domain.TbSyncAgriculturalProductLandmark;
import com.cr.sync.domain.dto.LsdCountParam;
import com.cr.sync.domain.vo.LsdAgriculturalProductLandmarkVo;
import com.cr.sync.mapper.TbSyncAgriculturalProductLandmarkMapper;
import com.cr.sync.service.ITbSyncAgriculturalProductLandmarkService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 远程数据-农产品地标Service业务层处理
 * 
 * @author tang
 * @date 2025-08-29
 */
@Service
public class TbSyncAgriculturalProductLandmarkServiceImpl implements ITbSyncAgriculturalProductLandmarkService 
{
    @Resource
    private TbSyncAgriculturalProductLandmarkMapper tbSyncAgriculturalProductLandmarkMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public void insertBatch(List<TbSyncAgriculturalProductLandmark> data) {
        if(data != null && !data.isEmpty()) {

            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            TbSyncAgriculturalProductLandmarkMapper mapper = sqlSession.getMapper(TbSyncAgriculturalProductLandmarkMapper.class);

            try {
                // 先删除旧数据
                mapper.deleteAll();

                // 添加新数据
                for(int i = 0; i < data.size(); i ++) {
                    mapper.insertTbSyncAgriculturalProductLandmark(data.get(i));

                    // 每500条提交一次事务
                    if (i > 0 && i % 500 == 0) {
                        sqlSession.flushStatements();
                        sqlSession.clearCache();
                    }
                }

                sqlSession.commit();
            } catch (Exception e) {
                sqlSession.rollback();
                throw new RuntimeException(e);
            } finally {
                sqlSession.close();
            }
        }
    }

    @Override
    public Map<String, Object> getAgriculturalProductLandmarkList(LsdCountParam param) {
        /*
        Integer page = param.getPage();
        Integer pageSize = param.getPageSize();
        if(page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        // 分页查询
        PageHelper.startPage(page, pageSize);
        List<LsdAgriculturalProductLandmarkVo> list = tbSyncAgriculturalProductLandmarkMapper.selectLandmarkList(param.getArea());
        PageInfo<LsdAgriculturalProductLandmarkVo> landmarkPageInfo = new PageInfo<>(list);
        PageHelper.clearPage();

        Map<String, Object> results = new HashMap<>();
        results.put("total", landmarkPageInfo.getTotal());
        results.put("data", landmarkPageInfo.getList());
         */

        String data = "[" +
                "{\"id\":1,\"brandName\":\"武川莜麦\",\"productName\":\"莜麦\",\"industry\":\"1\",\"productionScale\":\"2万\",\"productionScaleUnit\":\"公顷\",\"attYearOutput\":\"3.6万\",\"area\":\"武川县\",\"mainBodyName\":\"武川县农业技术推广中心\",\"issueDate\":\"2021-12-26\"}," +
                "{\"id\":2,\"brandName\":\"哈素海鲤鱼\",\"productName\":\"鲤鱼\",\"industry\":\"3\",\"productionScale\":\"0.3536万公顷\",\"productionScaleUnit\":\"公顷\",\"attYearOutput\":\"0.39万\",\"area\":\"土默特左旗\",\"mainBodyName\":\"土默特左旗农产品质量安全检测中心\",\"issueDate\":\"2021-06-04\"}," +
                "{\"id\":3,\"brandName\":\"清水河花菇\",\"productName\":\"香菇\",\"industry\":\"1\",\"productionScale\":\"0.05万\",\"productionScaleUnit\":\"座\",\"attYearOutput\":\"0.5万\",\"area\":\"清水河县\",\"mainBodyName\":\"清水河县食用菌协会\",\"issueDate\":\"2021-06-04\"}," +
                "{\"id\":4,\"brandName\":\"毕克齐大葱\",\"productName\":\"大葱\",\"industry\":\"1\",\"productionScale\":\"0.066万\",\"productionScaleUnit\":\"公顷\",\"attYearOutput\":\"3.6万\",\"area\":\"土默特左旗毕克齐镇\",\"mainBodyName\":\"土默特左旗农产品质量安全检测中心\",\"issueDate\":\"2020-04-30\"}" +
                "]";
        List<LsdAgriculturalProductLandmarkVo> list = JSON.parseArray(data, LsdAgriculturalProductLandmarkVo.class);
        // 区域过滤
        if(StringUtils.isNotBlank(param.getArea())) {
            list = list.stream()
                    .filter(vo -> vo.getArea().equals(param.getArea()))
                    .collect(Collectors.toList());
        }
        Map<String, Object> results = new HashMap<>();
        results.put("total", list.size());
        results.put("data", list);
        return results;

    }

}
